package leafedbox.commands
{
	import leafedbox.events.CommandEvent;
	import leafedbox.managers.CommandManager;
	
	import mx.rpc.http.HTTPService;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;

	import flash.events.Event;

	public class HttpRemoteCommand extends RemoteCommand
	{
		//----------------------------------------------------------------------
		// Constructor
		//----------------------------------------------------------------------
	
		/**
		 * Constructor.
		 */
		public function HttpRemoteCommand():void
		{
			super();

			// Setup generic service
			service = new HTTPService();
			service.method = 'GET';
			service.addEventListener(ResultEvent.RESULT, service_onResult);
			service.addEventListener(FaultEvent.FAULT, service_onFault);
		}
	

		//----------------------------------------------------------------------
		// Properties
		//----------------------------------------------------------------------
	
	    //----------------------------------
	    // General
	    //----------------------------------
	
		/**
		 * The remote service that is used as a transport.
		 */
		public var service : HTTPService;

		/** @private */
		protected var method : String = "GET";


	    //----------------------------------
	    // URL
	    //----------------------------------
	
		/** @private */
		protected var _url : String = "";
	
		/**
		 * The URL of the remote command.
		 */
		public function get url():String
		{
			return _url;
		}
	
		/** @private */
		public function set url(value:String):void
		{
			_url = value;
		}
	

		//----------------------------------------------------------------------
		// Methods : Command
		//----------------------------------------------------------------------
	
		/** @private */
		override public function execute():void
		{
			super.execute();

			// Retrieve global parameters
			var globalParameters : Object = CommandManager.getGlobalParameters();

			// Build URL string
			var key:String;
			var urlString : String = url;
			for(key in globalParameters['GET']) {
				urlString += (urlString.indexOf('?') == -1 ? '?' : '&') + key + '=' + escape(globalParameters['GET'][key]);
			}
			for(key in parameters['GET']) {
				urlString += (urlString.indexOf('?') == -1 ? '?' : '&') + key + '=' + escape(parameters['GET'][key]);
			}
		
			// Build POST data
			var data : Object = new Object();
			for(key in globalParameters['POST']) {
				data[key] = globalParameters['POST'][key];
			}
			for(key in parameters['POST']) {
				data[key] = parameters['POST'][key];
			}
		
			// Send data
			service.url = urlString;
			service.method = method;
			service.send(data);
	
			processInvoke(new CommandEvent(CommandEvent.INVOKE, false, true, this));
		}



		//----------------------------------------------------------------------
		// Events : RemoteCommand
		//----------------------------------------------------------------------
	
		/** @private */
		protected function service_onResult(event:ResultEvent):void
		{
			processResult(new CommandEvent(CommandEvent.RESULT, true, true, this));
		}

		/** @private */
		protected function service_onFault(event:FaultEvent):void
		{
			processFault(new CommandEvent(CommandEvent.FAULT, true, true, this));
		}

	}

}